package com.atguigu.mybatisplus.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
@Component
@Slf4j //日志
public class MyMetaObjectHandler implements MetaObjectHandler {

    //增加时
    @Override
    public void insertFill(MetaObject metaObject) {

        log.info("-----------insert自动填充添加");

        //自动填充业务逻辑
        this.strictInsertFill(metaObject,"createTime", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject,"updateTime", LocalDateTime.class, LocalDateTime.now());

        //判断实体类age是否为空，如果为空则自动填充值3,如果不为空,则执行自己设的值，
        Object age = this.getFieldValByName("age", metaObject);
        if (age == null){
            log.info("----------执行age");
            this.strictInsertFill(metaObject,"age",Integer.class,3);
        }

        //如果实体类里有string类型的author ,则继续执行自动填充
        boolean author = metaObject.hasSetter("author");
        if (author){
            log.info("**************author执行。 了。。");
            this.strictInsertFill(metaObject,"author",String.class,"石头");
        }

    }
    //修改时
    @Override
    public void updateFill(MetaObject metaObject) {

        this.strictUpdateFill(metaObject,"updateTime", LocalDateTime.class, LocalDateTime.now());
        log.info("---------update自动填充更新");


    }
}
